package com.test.nio.channel;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyChannel {


    public static void main(String args[]){
        try {
            RandomAccessFile fromFile = new RandomAccessFile("H:\\robots.txt","rw");
            FileChannel fromChannel = fromFile.getChannel();

            RandomAccessFile toFile = new RandomAccessFile("H:\\robots_copy.txt","rw");
            FileChannel toChannel = toFile.getChannel();

            long position = 0;
            long count = fromChannel.size();

            //复制到toChannel
            toChannel.transferFrom(fromChannel,position,count);
            System.out.println(toChannel.size());

            //或者
            //count = toChannel.size();
            //fromChannel.transferTo(position,count,toChannel);
            //System.out.println(toChannel.size());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
